package com.sj.command;

import com.sj.common.lang.Const;
import com.sj.pojo.MsgTypeEnum;
import com.sj.pojo.Protocol;
import com.sj.utils.TransferUtils;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoqi
 * Date: 2022-07-12
 * Time: 上午 10:54
 * Description: 字符串命令
 * 用来编写String命令的处理逻辑
 *
 * @author xiaoqi
 * @since 2022-07-12
 */
public class CommandString extends Command {
    
    private CommandString() {

    }

    public static Protocol doing(String msg) {
        return doString(msg);
    }

    private static Protocol doString(String msg) {
        String[] instruct = msg.split(" ");
        if (instruct.length <= 1) {
            return protocol.getProtocol(MsgTypeEnum.RESPONSE, "错误的命令!");
        } else if (Const.GET.equalsIgnoreCase(instruct[0])) {
            String res = dict.get(instruct[1]);
            if (res == null) {
                res = instruct[1] + " is undefine!";
            }
            return protocol.getProtocol(MsgTypeEnum.RESPONSE, res);
        } else if (Const.SET.equalsIgnoreCase(instruct[0])) {
            if (instruct.length < 3) {
                return protocol.getProtocol(MsgTypeEnum.RESPONSE, "错误的命令!");
            }
            dict.put(instruct[1], TransferUtils.joint(instruct, 2));
            return protocol.getProtocol(MsgTypeEnum.RESPONSE, "Ok, current size :" + dict.size());
        } else if (Const.EXISTS.equalsIgnoreCase(instruct[0])) {
            Protocol check = TransferUtils.check(instruct, 2);
            if (check != null) {
                return check;
            }
            String res = dict.get(instruct[1]);
            if (res == null) {
                return protocol.getProtocol(MsgTypeEnum.RESPONSE, "0");
            }
            return protocol.getProtocol(MsgTypeEnum.RESPONSE, "1");
        } else if (Const.APPEND.equalsIgnoreCase(instruct[0])) {
            if (instruct.length < 3) {
                return protocol.getProtocol(MsgTypeEnum.RESPONSE, "错误的命令!");
            }
            String orDefault = dict.getOrDefault(instruct[1], "");
            orDefault += TransferUtils.joint(instruct, 2);
            dict.put(instruct[1], orDefault);
            return protocol.getProtocol(MsgTypeEnum.RESPONSE, "Ok, current size :" + dict.size());
        } else if (Const.STRLEN.equalsIgnoreCase(instruct[0])) {
            Protocol check = TransferUtils.check(instruct, 2, true, dict);
            if (check != null) {
                return check;
            }
            return protocol.getProtocol(MsgTypeEnum.RESPONSE, dict.get(instruct[1]).length() + "");
        } else if (Const.INCR.equalsIgnoreCase(instruct[0])) {
            return TransferUtils.cyBy(instruct, 2, true, dict, 1);
        } else if (Const.DECR.equalsIgnoreCase(instruct[0])) {
            return TransferUtils.cyBy(instruct, 2, true, dict, -1);
        } else if (Const.DECRBY.equalsIgnoreCase(instruct[0])) {
            Protocol check = TransferUtils.check(instruct, 3, true, dict);
            if (check != null) {
                return check;
            }
            return TransferUtils.cyBy(instruct, 3, true, dict, Integer.parseInt(instruct[2]) * -1);
        } else if (Const.INCRBY.equalsIgnoreCase(instruct[0])) {
            Protocol check = TransferUtils.check(instruct, 3, true, dict);
            if (check != null) {
                return check;
            }
            return TransferUtils.cyBy(instruct, 3, true, dict, Integer.parseInt(instruct[2]));
        }
        return protocol.getProtocol(MsgTypeEnum.RESPONSE, "do nothing!");
    }
}